package protocol;

import java.util.Iterator;

import org.json.simple.JSONObject;

public class Cookies {
	private JSONObject fCookies;

	public Cookies() {
		fCookies = new JSONObject();
	}

	@SuppressWarnings("unchecked")
	public Cookies(String data) {
		this();
		if (data!=null && data.length()>2){
			String[] headers = data.split("\\s*;\\s*");
			for (String header : headers) {
				String[] keyval = header.split("=");
				if (keyval.length==2){
					fCookies.put(keyval[0], keyval[1]);
				}
				}
		}
	}

	public Object get(Object key) {
		return this.fCookies.get(key);
	}

	@SuppressWarnings("unchecked")
	public void put(Object key, Object value) {
		this.fCookies.put(key, value);
	}

	@SuppressWarnings("rawtypes")
	public String toString() {
		String ret = new String();
		for (Iterator iterator = fCookies.keySet().iterator(); iterator
				.hasNext();) {
			String key = (String) iterator.next();
			ret += key + "=" + fCookies.get(key) + "; ";
		}
		return ret;
	}
}
